### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata <- read.dta("Analysis Data/NIBRS_analysis.dta")

# Panel A: Baseline Temperature—Aggravated Assault Relationship

T1_agg <- feols(agg_assault_pc~ tMean + prec , data=mydata)
summary(T1_agg, cluster="fips")
T2_agg <- feols(agg_assault_pc~ tMean + prec | sample_month + week + dow, data=mydata)
summary(T2_agg, cluster="fips")
T3_agg <- feols(agg_assault_pc~ tMean + prec | state_sample_month + week + dow, data=mydata)
summary(T3_agg, cluster="fips")
T4_agg <- feols(agg_assault_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata)
summary(T4_agg, cluster="fips")

models <- list(T1_agg, T2_agg, T3_agg, T4_agg)
modelsummary(models, vcov = ~fips,  output = "Figures and Tables/Table_C2/Table_C2_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel B: Baseline Temperature—Aggravated Assault Relationship involving a Firearm

T1_agg_g <- feols(gun_agg_assault_pc~ tMean + prec , data=mydata)
summary(T1_agg_g)
T2_agg_g <- feols(gun_agg_assault_pc~ tMean + prec | sample_month + week + dow, data=mydata)
summary(T2_agg_g, cluster="fips")
T3_agg_g <- feols(gun_agg_assault_pc~ tMean + prec | state_sample_month + week + dow, data=mydata)
summary(T3_agg_g, cluster="fips")
T4_agg_g <- feols(gun_agg_assault_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata)
summary(T4_agg_g, cluster="fips")

models <- list(T1_agg_g, T2_agg_g, T3_agg_g, T4_agg_g)
modelsummary(models, vcov = ~fips,  output = "Figures and Tables/Table_C2/Table_C2_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel C: Baseline Temperature—Aggravated Assault Relationship not involving a Firearm

T1_agg_ng <- feols(ngun_agg_assault_pc~ tMean + prec , data=mydata)
summary(T1_agg_ng)
T2_agg_ng <- feols(ngun_agg_assault_pc~ tMean + prec | sample_month + week + dow, data=mydata)
summary(T2_agg_ng, cluster="fips")
T3_agg_ng <- feols(ngun_agg_assault_pc~ tMean + prec | state_sample_month + week + dow, data=mydata)
summary(T3_agg_ng, cluster="fips")
T4_agg_ng <- feols(ngun_agg_assault_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata)
summary(T4_agg_ng, cluster="fips")

models <- list(T1_agg_ng, T2_agg_ng, T3_agg_ng, T4_agg_ng)
modelsummary(models, vcov = ~fips,  output = "Figures and Tables/Table_C2/Table_C2_Panel_C.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))
